/**
 * 
 */
package com.delta.rafg.core.constants;


import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import com.delta.rafg.core.base.AWorkerBean;
import com.delta.rafg.core.exception.RAFGException;
import com.delta.rafg.coredata.ClientData;


/**
 * @author RAFG Development Team (D9785692)
 */
public class RAFGResourceBundle extends AWorkerBean {

    private static final long serialVersionUID = -1563262798993707437L;

    private static Map<Locale, ResourceBundle> localeBundleCache =
        new HashMap<Locale, ResourceBundle>();

    /**
     * Returns the message for the code based on the Client Locale.
     * @param pCode
     * @param pClientData
     * @return
     * @throws RAFGException
     */
    public static String getMessage(final String pCode,
        final ClientData pClientData) {

        Locale theLocale = null;
        if (pClientData != null && pClientData.getLocale() != null) {
            theLocale = new Locale(pClientData.getLocale());
        }
        if (theLocale == null) {
            theLocale = Locale.US;
        }
        ResourceBundle theBundle = localeBundleCache.get(theLocale);
        String theLocalMessage = pCode;
        if (theBundle == null) {
            try {
                theBundle = ResourceBundle.getBundle("RAFGMessages", theLocale);
                localeBundleCache.put(theLocale, theBundle);
            } catch (final MissingResourceException e) {
                // new RAFGException(e);
            }
        }
        if (theBundle != null) {
            theLocalMessage = theBundle.getString(pCode);
        }
        return theLocalMessage;
    }
}
